package com.example.mcpclient.controller;

import com.example.mcpclient.request.InputMsgRequest;
import com.example.mcpclient.service.IntendService;
import org.springframework.ai.chat.client.ChatClient;
import org.springframework.ai.chat.memory.ChatMemory;
import org.springframework.ai.chat.model.ChatResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;

/**
 * 对常用的对话场景的测试, 不包含工具和向量
 *
 * @author xuguang
 */
@RestController
@RequestMapping("/chat")
public class ChatController {

    @Autowired
    private ChatClient chatClient;
    @Autowired
    private IntendService intendService;


    /**
     * 1.1 流式的方式输出
     */
    @PostMapping(value = "/stream", produces = MediaType.TEXT_EVENT_STREAM_VALUE)
    public Flux<String> stream(@RequestBody InputMsgRequest userInput) {
        return chatClient
                .prompt(userInput.getQuestion())
                .system("You are a helpful assistant.")
                .stream().content();
    }

    /**
     * 1.2 直接方式输出
     */
    @PostMapping(value = "/direct")
    public Flux<String> direct(@RequestBody InputMsgRequest userInput) {
        return chatClient
                .prompt(userInput.getQuestion())
                .stream().content();
    }


    /**
     * 2.1 ai原始信息
     */
    @PostMapping("/guessIntentWithOriginal")
    public Flux<ChatResponse> identifyIntent(@RequestBody InputMsgRequest userInput) {
        return intendService.guessIntentWithOriginal(userInput.getQuestion());
    }

    /**
     * 2.2 意图识别
     */
    @PostMapping("/guessIntent")
    public Mono<String> guessIntent(@RequestBody InputMsgRequest userInput) {
        return intendService.guessIntent(userInput.getQuestion());
    }

    /**
     * 2.2 考虑曾经的历史会话 by 内存来存
     */
    @PostMapping("/history")
    public Flux<String> history(@RequestBody InputMsgRequest userInput) {
        // 使用会话ID确保对话历史正确关联
        String conversationId = userInput.getConversationId() != null ?
                userInput.getConversationId() :
                "conversation-" + System.currentTimeMillis();
        return chatClient
                .prompt(userInput.getQuestion())
                .advisors(advisor -> advisor.param(ChatMemory.CONVERSATION_ID, conversationId))
                .stream().content();
    }


    /**
     * 3.1 prompt的使用
     */
    @PostMapping("/translate")
    public Flux<String> translate(@RequestBody InputMsgRequest userInput) {
        return chatClient
                .prompt()
                .user("请翻译如下内容:{text}")
                .user(promptUserSpec -> promptUserSpec.param("text", userInput.getQuestion()))
                .system("""
                        你是一个翻译家，请将用户输入的中文翻译成英文。
                        回复格式要求：
                        1. 第一行输出"原文 :[用户输入的内容]"
                        2. 换行后输出"译文 :[你的翻译结果]"
                        3. 不要包含其他内容
                        """)
                .stream().content();
    }


}
